package com.yijiaganxi.services.web.notice;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yijiaganxi.common.entity.notice.YjgxNotice;
import org.yijiaganxi.common.res.R;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.yijiaganxi.common.GlobalConstants;
import com.yijiaganxi.services.service.notice.YjgxNoticeService;
import java.util.Date;
import javax.validation.constraints.NotBlank;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;

/**
 * <p>
 * 公告管理 前端控制器
 * </p>
 *
 * @author zhangguowei123
 * @since 2020-08-29
 */
@Validated
@RestController
@RequestMapping("/services/yjgxNotice/"+GlobalConstants.WEB_FLAG)
public class YjgxNoticeController {

	@Autowired
	public YjgxNoticeService  noteService;
	

	/**
	 * 获取所有公告
	 * @return
	 */
	@GetMapping("getAllNoticeList")
	public R getAllNoteList() {
		return R.opertionListResponse( noteService.selectList(new EntityWrapper<YjgxNotice>()));
	}
	
	/**
	 * 根据条件查询公告
	 * @return
	 */
	@GetMapping("getNoticeById")
	public Object getNoticeById(@NotBlank(message = "id不能为空") String id) {
		return R.opertionPojoResponse( noteService.selectById(id));
	}
	
	/**
	 * 添加公告
	 * @param banner
	 * @return
	 */
	@PostMapping("addNotice")
	public R addNotice(@Validated YjgxNotice notice) {
		return R.insertOrUpdateResponse(noteService.insert(notice));
	}
	
	/**
	 * 更新公告
	 * @param banner
	 * @return
	 */
	@PostMapping("updateNotice")
	public R updateNotice(YjgxNotice notice) {
		if(notice.getId()==null)
			return R.error("Id 更新操作必填");
		notice.setModifyDate(new Date());
		return R.insertOrUpdateResponse(noteService.update(notice, new EntityWrapper<YjgxNotice>()
				.eq("id", notice.getId())));
	}
	
	/**
	 * 删除轮播
	 * @param id
	 * @return
	 */
	@PostMapping("deleteNotice")
	public R deleteNotice(@NotBlank(message = "id不能为空") String id) {
		return R.insertOrUpdateResponse(noteService.deleteById(id));
	}
}

